package com.example.shoppingdemo.RKL;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shoppingdemo.MainActivity;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.RKL.Model.LoginModel;
import com.example.shoppingdemo.RKL.Model.MsgModel;
import com.example.shoppingdemo.RKL.Persenter.PersenterLoginAngSigin;
import com.example.shoppingdemo.Util.CodeUtil;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/*
 * 登录
 * */
public class Login_Splash_Activity extends AppCompatActivity implements View.OnClickListener {
    //登录账号及密码
    private EditText loginId;
    private EditText loginPassword;
    //验证码
    private String realCode;
    private EditText writeMsg;
    private TextView  showMsg;
    //登录按钮
    private Button logbtn;
    //记住密码
    private CheckBox remember;
    private Button fillin;
    private SharedPreferences preferences;
    private PersenterLoginAngSigin persenterLoginAngSigin;
    private TextView mamend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spiash_login);
        persenterLoginAngSigin=new PersenterLoginAngSigin();
        logbtn=findViewById(R.id.btn_login);
        findViewById(R.id.go_enroll).setOnClickListener(this);
        //记住密码
        remember=findViewById(R.id.rem_id);
        //自动填写
        fillin=findViewById(R.id.fillin_id);
        preferences=getSharedPreferences("shopping_shar",MODE_PRIVATE);
        /*记住密码方法*/
        myremember();
        /*判断内容及跳转方法*/
        iogin();
        //验证码方法
        myMsg();
        //跳转修改密码
        MyAmend();
    }

    private void MyAmend() {
        mamend = findViewById(R.id.forget_password);
        mamend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Login_Splash_Activity.this,AmendActivity.class));
            }
        });
    }

    private void myremember() {
        remember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b==true){
                    //登录账号
                    loginId = findViewById(R.id.lonin_id);
                    //密码
                    loginPassword = findViewById(R.id.login_password);
                    String z=loginId.getText().toString();
                    String m=loginPassword.getText().toString();
                    if (TextUtils.isEmpty(z)|TextUtils.isEmpty(m)){
                        Toast.makeText(Login_Splash_Activity.this,"请输入账号及密码",Toast.LENGTH_SHORT).show();
                    }else {
                        SharedPreferences.Editor editor=preferences.edit();
                        editor.putLong("zz", Long.parseLong(z));
                        editor.putLong("mm", Long.parseLong(m));
                        editor.commit();
                        Toast.makeText(Login_Splash_Activity.this,"账号及密码已保存",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(Login_Splash_Activity.this,"请登录",Toast.LENGTH_SHORT).show();

                }
            }
        });
        fillin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //登录账号
                loginId = findViewById(R.id.lonin_id);
                //密码
                loginPassword = findViewById(R.id.login_password);
                long z1 = preferences.getLong("zz", 0l);
                if (z1 != 0l) {
                    loginId.setText(String.valueOf(z1));
                }
                long z2 = preferences.getLong("mm", 0l);
                if (z2 != 0l) {
                    loginPassword.setText(String.valueOf(z2));
                }
            }
        });
    }


    /*验证码方法*/
    private void myMsg() {
        //随机生成验证码
        showMsg = findViewById(R.id.tu_msg);

        showMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText mphone = findViewById(R.id.lonin_id);
                String phone=mphone.getText().toString();
                persenterLoginAngSigin.persenterMsg(phone, new PersenterLoginAngSigin.TransferMsg() {
                    @Override
                    public void TransferMsg(List<MsgModel> msgModelList) {
                        String s = msgModelList.get(0).getCode();
                        Toast.makeText(Login_Splash_Activity.this, s, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        /*showMsg.setImageBitmap(CodeUtil.getInstance().createBitmap());
        //将验证码用图片的形式显示出来
        realCode = CodeUtil.getInstance().getCode().toLowerCase();
        //点击随机生成验证码
        showMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMsg.setImageBitmap(CodeUtil.getInstance().createBitmap());
                //将验证码用图片的形式显示出来
                realCode = CodeUtil.getInstance().getCode().toLowerCase();
            }
        });*/
    }
    /*判断内容及跳转方法*/
    private void iogin() {
        logbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            //登录账号
            loginId = findViewById(R.id.lonin_id);
            //密码
            loginPassword = findViewById(R.id.login_password);
            //验证码
            writeMsg = findViewById(R.id.input);
            //转换成String
            String name = loginId.getText().toString();
            String Password = loginPassword.getText().toString();
            String code = writeMsg.getText().toString();
            //系统自带的判空方法，可判断两种空：""和null
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(Login_Splash_Activity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            }else if (!checkPhone(name)|name.length() != 11) {
                Toast.makeText(Login_Splash_Activity.this, "手机号格式错误", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(Password)) {
                Toast.makeText(Login_Splash_Activity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(code)) {
                Toast.makeText(Login_Splash_Activity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            }  else {
                persenterLoginAngSigin.persenterLogin(name, Password, code, "xx", new PersenterLoginAngSigin.TransferLogin() {
                    @Override
                    public void TransferLogin(List<LoginModel> loginModelList) {
                        String dlBean = loginModelList.get(0).toString();
                        if (dlBean!=null) {
                            if (dlBean.equals("登录失败")) {
                                Toast.makeText(Login_Splash_Activity.this, dlBean, Toast.LENGTH_SHORT).show();
                            }else {
                                Intent intent = new Intent(Login_Splash_Activity.this, MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                Toast.makeText(Login_Splash_Activity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                startActivity(intent);
                            }
                        } else {
                            Toast.makeText(Login_Splash_Activity.this, "手机号未注册", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
              /*  Intent intent=new Intent(Login_Splash_Activity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                Toast.makeText(Login_Splash_Activity.this, "登录成功", Toast.LENGTH_SHORT).show();
                startActivity(intent);*/
            }
            }
        });
    }

    //判断手机号格式方法
    public static boolean checkPhone(String phone) {
        Pattern pattern = Pattern
                .compile("^(13[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\\d{8}$");
        Matcher matcher = pattern.matcher(phone);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.go_enroll:
                Intent intent2=new Intent(Login_Splash_Activity.this,SigInActivity.class);
                startActivity(intent2);
                break;
        }
    }
}